package leecode.q1396;

import java.util.*;

public class UndergroundSystem {
    class Data {
        int shangcheshijian;
        int xiacheshijain;
        String startStation;
        String endStation;

        public Data(String stationName, int t) {
            this.startStation = stationName;
            this.shangcheshijian = t;
        }
    }

    Map<Integer, Data> map = new HashMap<>();

    public UndergroundSystem() {

    }

    public void checkIn(int id, String stationName, int t) {
        map.put(id, new Data(stationName, t));
    }

    public void checkOut(int id, String stationName, int t) {
        Data data = map.get(id);
        data.endStation = stationName;
        data.xiacheshijain = t;
    }

    public double getAverageTime(String startStation, String endStation) {
        double sum = 0;
        int i = 0;
        for (Map.Entry<Integer, Data> entry : map.entrySet()) {
            Data value = entry.getValue();
            if (startStation.equals(value.startStation) && endStation.equals(value.endStation)) {
                int re = value.xiacheshijain - value.shangcheshijian;
                sum += re;
                i++;
            }
        }
        return sum / i;
    }

    public boolean isFirstBearWin(int[] nums) {
        int picker = picker(0, nums.length - 1, nums);
        System.out.println(picker);
        return picker > 0;
    }

    private int picker(int i, int j, int[] nums) {
        if (i == j) {
            System.out.println("i = " + i);
            return nums[i];
        }
        int head = nums[i] - picker(i + 1, j, nums);
        int end = nums[j] - picker(i, j - 1, nums);
        return Math.max(head, end);
    }

    List<Integer> list = new ArrayList<>();

    public void fun() {
        Collections.reverse(list);
    }

    boolean[][] array;

    public List<List<String>> solveNQueens(int n) {
        array = new boolean[n][n];
        cacl(0, n, array);
        return null;
    }

    private void cacl(int index, int n, boolean[][] array) {
        if (index == n) {
            array = new boolean[n][n];
            return;
        }
        for (int i = 0; i < n; i++) {
            if (isOk(index, i, array)) {
                array[index][i] = true;
                cacl(i++, n, array);
            }
        }
    }

    int res = 0;

    public int solveNQueens1(int n) {
        for (int i = 0; i < n; i++) {
            boolean[][] array = new boolean[n][n];
            cacl1(i++, 1, array);
        }
        return 0;
    }

    private void cacl1(int i, int y, boolean[][] array) {
        if (y == array.length) {
            return;
        }
        if (isOk(i, y, array)) {
            array[i][y] = true;
            if (y == array.length - 1) {
                res++;
            }
            cacl1(i + 1, y + 1, array);
        }
    }

    //
    boolean isOk(int hang, int lie, boolean[][] array) {
        // 竖向是否有
        for (int i = 0; i < hang; i++) {
            if (array[i][lie]) {
                return false;
            }
        }
        // 左上是否有
        int i1 = hang;
        int i2 = lie;
        while (i1 > 0 && i2 > 0) {
            if (array[--i1][--i2]) {
                return false;
            }
        }
        // 右上是否有
        int i11 = hang;
        int i22 = lie;
        while (i11 >= 0 && i22 < array.length) {
            if (array[i11][i22]) {
                return false;
            }
            i11--;
            i22++;
        }
        return true;
    }
}
